package com.spic.business.vehicle.carInfo.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.spic.business.alarm.constant.CommonConstant;
import com.spic.business.alarm.utils.HttpClientUtil;
import com.spic.business.vehicle.carInfo.constant.CarConstant;
import com.spic.business.vehicle.carInfo.domain.CarInfoDto;
import com.spic.business.vehicle.carInfo.service.ICarInfoservice;
import com.spic.business.vehicle.domain.VehicleManagement;
import com.spic.business.vehicle.service.IVehicleManagementService;
import com.spic.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : 赵祥
 * create at:  2022/11/4  17:24
 * @Description :
 **/
@Service
public class ZYCLCarInfoServiceImpl implements ICarInfoservice {
    @Autowired
    private IVehicleManagementService vehicleManagementService;
    @Override
    public List<CarInfoDto> selectCarList() {
        /**
         * {
         *     "cmd":"queryCorpDeptVehicleList",
         *     "auth": {
         *         "devKey": "05b8d4b7f2e606c47d6d0deb05b10cf3"
         *     },
         *     "params":{
         *         "corpId": "82b0873fd9a741c9a32cf0059e73f45b",
         *         "deptId": "82b0873fd9a741c9a32cf0059e73f45b",
         *         "pageSize": "1000"
         *     }
         * }
         */
        JSONObject paramMap = new JSONObject();
        paramMap.put("cmd", "queryCorpDeptVehicleList");
        JSONObject devKey = new JSONObject();
        devKey.put("devKey", "05b8d4b7f2e606c47d6d0deb05b10cf3");
        paramMap.put("auth", devKey);
        JSONObject params = new JSONObject();
        params.put("corpId", CarConstant.CORP_ID);
        params.put("deptId", CarConstant.DEPT_ID);
        params.put("pageSize", 1000);
        paramMap.put("params", params);

        String jsonStr = HttpUtil.post(CarConstant.ZYCL_URL, paramMap.toJSONString(), 5000);
        JSONObject resultJson = JSONObject.parseObject(jsonStr);
        if (resultJson.getInteger("result") == 0) {
            JSONArray jsonArray = resultJson.getJSONObject("detail").getJSONArray("dataList");
            List<CarInfoDto> dtoList = JSONArray.parseArray(jsonArray.toJSONString(), CarInfoDto.class);
            return dtoList;
        }
        return null;
    }

    @Override
    public JSONObject getUserMarkers() {
        return null;
    }

    public void syncOilWear() {
        JSONObject paramMap = new JSONObject();
        paramMap.put("cmd", "queryCorpDeptVehicleList");
        JSONObject devKey = new JSONObject();
        devKey.put("devKey", "05b8d4b7f2e606c47d6d0deb05b10cf3");
        paramMap.put("auth", devKey);
        JSONObject params = new JSONObject();
        params.put("corpId", CarConstant.CORP_ID);
        params.put("deptId", CarConstant.DEPT_ID);
        params.put("pageSize", 1000);
        paramMap.put("params", params);

        String jsonStr = HttpUtil.post(CarConstant.ZYCL_URL, paramMap.toJSONString(), 5000);
        JSONObject resultJson = JSONObject.parseObject(jsonStr);
        if (resultJson.getInteger("result") == 0) {
            JSONArray jsonArray = resultJson.getJSONObject("detail").getJSONArray("dataList");
            List<CarInfoDto> dtoList = JSONArray.parseArray(jsonArray.toJSONString(), CarInfoDto.class);
            for (CarInfoDto carInfo : dtoList) {
                String licenseplate = carInfo.getLicenseplate();
                String fuelConsumption = carInfo.getFuelConsumption();
                String distanceTotal = carInfo.getDistanceTotal();
                String longitude = carInfo.getLongitude();
                String latitude = carInfo.getLatitude();
                VehicleManagement vehicleManagement = new VehicleManagement();
                vehicleManagement.setPlateno(licenseplate);
                List<VehicleManagement> vehicleManagements = vehicleManagementService.selectVehicleManagementList(vehicleManagement);
                if(vehicleManagements.size() > 0) {
                    vehicleManagements.get(0).setFuelConsumption(fuelConsumption);
                    vehicleManagements.get(0).setDistanceTotal(distanceTotal);
                    vehicleManagements.get(0).setLongitude(longitude);
                    vehicleManagements.get(0).setLatitude(latitude);
                    vehicleManagementService.updateVehicleManagement(vehicleManagements.get(0));
                }
            }
        }
    }
}
